<?php
/*
列表页字段设置类
*/
namespace control;

class installSearchClass extends adIndexClass
{
    /*页面*/
    public function home()
    {
        //列字段列表
        $installList = $this->installList(['page' => $this->post['page']]);
        $left_columns = isset($installList['left_columns'])&&$installList['left_columns']?$installList['left_columns']:0;
        $type = array("私有");
        //生成页面
        $html = "
		<form name='install' class='install' page='" . $this->post['page'] . "' onsubmit='return false'>
    		<ul class='menu'>
    		    <li>
    		        <label title='全选' class='listCheckbox'>
    		            <input type='checkbox' name='name' form='install' onchange=\"checkAll(this)\" " . $installList['checkAll'] . ">
    		            <span></span>
                    </label>
                </li>
    		    <li>列名称</li>
    		    <li><span class='menu-title'>别名</span></li>
    		    <li><span class='menu-title'>序号</span></li>
    		    <li><span class='menu-title'>列宽px</span></li>
    		</ul>
    		<div class='body'>" . $installList['body'] . "</div>
    		<div class='foot'>" .
                text(array("name" => "search", "class" => "width150", "title" => "查询列名称", "keyup" => "installSearchEnter")) .
                $this->dominate(array("detail" => array("title" => "查询列名称", "click" => "installSearch()"))) . "
    		    <span id='installSearchSpan'>0/0</span>
    		    &nbsp;&nbsp;固定前" . text(array("name" => "left_columns", "type" => "number", "class" => "width35 center", "other" => " min='0' max='{$installList['columns']}' step='1' ", "value" => $left_columns)) ."列
    		</div>
    		<div class='restoreDefault' onclick=\"restoreDefault()\">恢复默认设置</div>
    		<div class='confirm' onclick=\"installConfirm()\">确定</div>
		</form>
		";
        //返回弹出层
        return $this->layer(array("title" => "列名称设置", "height" => 500, "width" => 900, "html" => $html, "button" => false));
    }

    /*列字段列表
    $data = array(
        "stid" => "",//员工ID
    );
    */
    public function installList($data)
    {
        $pageArr = explode('-', $data['page']);
        //默认配置
        $className = $pageArr[0] . 'Class';
        $class     = new (__NAMESPACE__ . '\\' . $className)();
        $default   = $class->fieldInit($pageArr[1]);

        // 查找自定义显示字段
        $userSetting = query('user_setting', generateWhere([
            'type' => '自定义显示字段',
            'page' => $data['page'],
            'stid' => self::$stid,
        ]), 'id, value');
        // 加载数据库配置
        if ($userSetting['id'] && !isset($data['reset'])) {
            $setting = json_decode($userSetting['value'], true);
            //查看是否增加了默认字段
            $defaultKeys = array_keys($default);
            $userSettingKeys = array_keys($setting);
            $extraValues = array_diff($defaultKeys, $userSettingKeys);
            if(count((array)$extraValues) > 0) {
                foreach ((array)$extraValues as $key) {
                    // 寻找新增字段在数组中的位置
                    $keyIndex         = array_search($key, array_keys($default));
                    $array            = $default[$key];
                    $array['display'] = 0;
                    // 在对应位置的上一个索引处插入一个新的数组
                    $newArray = [
                        $key => $array,
                    ];
                    $setting = array_merge(array_slice($setting, 0, $keyIndex), $newArray, array_slice($setting, $keyIndex));
                }
            }
        } else {
            $setting = $default;
        }

        //参数准备
        $result     = array("body" => "", "checkAll" => "checked");
        $LatestList = 0;
        $left_columns = 0; //固定左前几列
        //遍历字段列表
        foreach ((array)$setting as $key => $installSearch) {
            //获取列名称，并替换多语言
            $name       = isset($default[$key]['name']) ?
                (str_contains($default[$key]['name'], '{l.') ? parseLangTag($default[$key]['name']) : $default[$key]['name']) :
                '';
            if(isset($installSearch['fix']) && $installSearch['fix']){
                $left_columns++;
            }
            if($name) {
                //$list       = isset($installSearch['list']) ? $installSearch['list'] : $LatestList + 1;
                $list       = $LatestList + 1;
                $alias = isset($installSearch['alias']) && $installSearch['alias'] ? $installSearch['alias'] : $name;
                $LatestList = $list;
                $other      = " onchange='installBox()' data-name='{$name}' ";
                if (isset($installSearch['display']) && empty($installSearch['display'])) {
                    $result['checkAll'] = "";
                } else {
                    $other .= " checked ";
                }
                $check          = $this->dominate(array("box" => array("title" => "勾选后显示此列", "name" => "name", "value" => $key, "other" => $other)));
                $result['body'] .= "
		    <ul>
		        <li>" . $check . "</li>
		        <li title='" . $name . "'>" . $name . "</li>
		        <li>" . text(array("name" => "alias", "value" => $alias)) . "</li>
		        <li>" . text(array("type" => "number", "name" => "list", "change" => "installList", "value" => $list, "other" => " min='0' max='1000000' step='1' ")) . "</li>
		        <li>" . text(array("type" => "number", "name" => suiji(), "value" => $installSearch['width'], "other" => " min='0' max='1000000' step='1' ")) . "</li>
		    </ul>
		    ";
            }
        }
        $result['columns'] = count((array)$setting)-1;
        $result['left_columns'] = $left_columns;
        //返回
        return $result;
    }

    /*恢复默认设置-删除个人配置*/
    public function restoreDefault()
    {
        $pageStr = $this->post['page'];
        if (empty($pageStr)) {
            $json['warn'] = "页面名称为空";
        } else {
            /*delete(array(
                "table" => 'user_setting',
                    "where" => generateWhere([
                    'type' => '自定义显示字段',
                    'page' => $pageStr,
                    'stid' => self::$stid,
                ])
            ));
            //提示
            $_SESSION['tip'] = "已恢复默认设置";*/
            $json['data'] = $this->installList(['page' => $pageStr, 'reset' => 1]);
            $json['warn']    = 2;
        }
        //返回
        return json_encode($json, 320);
    }

    /*提交*/
    public function installConfirm()
    {
        $pageStr = $this->post['page'];
        $left_columns = $this->post['left_columns'];
        if (empty($pageStr)) {
            $json['warn'] = "页面名称为空";
        } elseif (empty($this->post['install'])) {
            $json['warn'] = "列字段为空";
        } elseif ($left_columns >= count($this->post['install'])) {
            $json['warn'] = "固定列不能大于等于总列数";
        } else {
            // 设置固定左前列
            if($left_columns && $left_columns > 0){
                $keys = array_keys($this->post['install']);
                for ($i=0;$i<$left_columns;$i++){
                    $this->post['install'][$keys[$i]]['fix'] = 1;
                }
            }
            // 查找自定义显示字段
            $userSetting = query('user_setting', generateWhere([
                'type' => '自定义显示字段',
                'page' => $pageStr,
                'stid' => self::$stid,
            ]), 'id, value');
            // 加载数据库配置
            if ($userSetting['id']) {
                //更新
                update(array(
                    "table" => "user_setting",
                    "field" => array(
                        "value" => json_encode($this->post['install'], 320),
                    ),
                    "where" => " stid = '" . self::$stid . "' and page = '$pageStr' and type = '自定义显示字段' ",
                ));
            } else {
                insert([
                    "table" => 'user_setting',
                    "field" => [
                        'type'  => '自定义显示字段',
                        'stid'  => self::$stid,
                        'value' => json_encode($this->post['install'], 320),
                        'page'  => $pageStr,
                    ],
                ]);
            }
            //提示
            $_SESSION['tip'] = "列名称设置成功";
            $json['warn']    = 2;
        }
        //返回
        return json_encode($json, 320);
    }

    /*保存列宽*/
    public function width()
    {
        $pageStr = $this->post['page'];
        $pageArr = explode('-', $pageStr);
        $page    = $pageArr[0];
        //判断
        if (empty($pageStr)) {
            $json['warn'] = "页面名称为空";
        } elseif (empty($this->post['name'])) {
            $json['warn'] = "列字段名称为空";
        } elseif (empty($this->post['width'])) {
            $json['warn'] = "列宽为空";
        } elseif (preg_match(checkPrice, $this->post['width']) == 0) {
            $json['warn'] = "列宽只能为正整数";
        } else {
            $newSetting = [];
            // 查找自定义显示字段
            $userSetting = query('user_setting', generateWhere([
                'type' => '自定义显示字段',
                'page' => $pageStr,
                'stid' => self::$stid,
            ]), 'id, value');
            // 加载数据库配置
            if ($userSetting['id']) {
                $setting = json_decode($userSetting['value'], true);
                //生成此页面的私有字段设定
                foreach ($setting as $key => $installSearch) {
                    //判断列宽
                    if ($this->post['name'] == $key) {
                        $installSearch['width'] = $this->post['width'];
                    }
                    $newSetting[$key] = $installSearch;
                }
                //更新
                update(array(
                    "table" => "user_setting",
                    "field" => array(
                        "value" => json_encode($newSetting, 320),
                    ),
                    "where" => " stid = '" . self::$stid . "' and page = '$pageStr' and type = '自定义显示字段' ",
                ));
            } else {
                $className = $page . 'Class';
                //实例化对象
                $class = new (__NAMESPACE__ . '\\' . $className)();
                //打印结果
                $setting = $class->fieldInit($pageArr[1]);
                $list = 0;
                //生成此页面的私有字段设定
                foreach ($setting as $key => $installSearch) {
                    $installSearch['alias'] = $installSearch['name'];
                    unset($installSearch['name']);
                    $installSearch['display'] = 1;
                    $installSearch['list'] = $list = $list+1;
                    //判断列宽
                    if ($this->post['name'] == $key) {
                        $installSearch['width'] = $this->post['width'];
                    }
                    $newSetting[$key] = $installSearch;
                }
                insert([
                    "table" => 'user_setting',
                    "field" => [
                        'type'  => '自定义显示字段',
                        'stid'  => self::$stid,
                        'value' => json_encode($newSetting, 320),
                        'page'  => $pageStr,
                    ],
                ]);
            }
            //提示
            $json = array("warn" => 2, "tip" => "列字段设置成功");
        }
        //返回
        return json_encode($json, 320);
    }

    /*保存排序项*/
    public function saveSort()
    {
        $pageStr = $this->post['pageStr'];
        $list_sort_key = $this->post['list_sort_key'];
        $list_sort = $this->post['list_sort'];
        $pageArr = explode('-', $pageStr);
        $page    = $pageArr[0];
        //判断
        if (empty($pageStr)) {
            $json['warn'] = "页面名称为空";
        } elseif (empty($list_sort_key)) {
            $json['warn'] = "列排序字段名称为空";
        } elseif (empty($list_sort)) {
            $json['warn'] = "列排序方向";
        } else {
            $newSetting = [];
            // 查找自定义显示字段
            $userSetting = query('user_setting', generateWhere([
                'type' => '自定义显示字段',
                'page' => $pageStr,
                'stid' => self::$stid,
            ]), 'id, value');
            // 加载数据库配置
            if ($userSetting['id']) {
                $setting = json_decode($userSetting['value'], true);
                //生成此页面的私有字段设定
                foreach ($setting as $key => $installSearch) {
                    //判断列宽
                    if ($list_sort_key == $key) {
                        $installSearch['list_sort'] = $list_sort;
                    }else{
                        unset($installSearch['list_sort']);
                    }
                    $newSetting[$key] = $installSearch;
                }
                //更新
                update(array(
                    "table" => "user_setting",
                    "field" => array(
                        "value" => json_encode($newSetting, 320),
                    ),
                    "where" => " stid = '" . self::$stid . "' and page = '$pageStr' and type = '自定义显示字段' ",
                ));
            } else {
                $className = $page . 'Class';
                //实例化对象
                $class = new (__NAMESPACE__ . '\\' . $className)();
                //打印结果
                $setting = $class->fieldInit($pageArr[1]);
                $list = 0;
                //生成此页面的私有字段设定
                foreach ($setting as $key => $installSearch) {
                    $installSearch['alias'] = $installSearch['name'];
                    unset($installSearch['name']);
                    $installSearch['display'] = 1;
                    $installSearch['list'] = $list = $list+1;
                    //判断列宽
                    if ($list_sort_key == $key) {
                        $installSearch['list_sort'] = $list_sort;
                    }
                    $newSetting[$key] = $installSearch;
                }
                insert([
                    "table" => 'user_setting',
                    "field" => [
                        'type'  => '自定义显示字段',
                        'stid'  => self::$stid,
                        'value' => json_encode($newSetting, 320),
                        'page'  => $pageStr,
                    ],
                ]);
            }
            //提示
            $json = array("warn" => 2, "tip" => "设置默认排序成功");
        }
        //返回
        return json_encode($json, 320);
    }
}

?>
